package com.mtq.web.userlogin.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mtq.utils.ResponseResult;
import com.mtq.utils.SystemStatus;
import com.mtq.web.userlogin.service.UserLoginService;

@Controller
@RequestMapping("/user")
public class UserLoginController {
	private static final Logger logger = LoggerFactory
			.getLogger(UserLoginController.class);
			
	@Autowired
	private UserLoginService userLoginService;

	@RequestMapping(value = "/login")
	@ResponseBody
	public ResponseResult userLogin(HttpSession session,ResponseResult responseResult,
			String username,String password) {
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			responseResult.setState(SystemStatus.REQUEST_ERROR);
			responseResult.setMsg("账号和密码不能为空");
		}else {
			try {
				Map<String,Object> user = userLoginService.loginQuery(username, password);
				if(user != null) {
					session.setAttribute("user", user);
					responseResult.setState(SystemStatus.REQUEST_SUCCESS);
					responseResult.setMsg("登录成功");
				} else {
					responseResult.setState(SystemStatus.REQUEST_ERROR);
					responseResult.setMsg("用户名或密码错误");
				}
			} catch (Exception e) {
				logger.error("用户登录出错！！【错误提示】："+e.getMessage());
				responseResult.setState(SystemStatus.REQUEST_ERROR);
				responseResult.setMsg("用户登录出错");
				e.printStackTrace();
			}
		}
		return responseResult;
	}
}
